﻿using EventModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace ConsumeBus
{
    public class ConsumeRepository : IConsumeRepository
    {
        ExamTool _examTool;
        RedisHelper _redisHelper;

        public ConsumeRepository(ExamTool examTool, RedisHelper redisHelper)
        {
            this._examTool = examTool;
            this._redisHelper = redisHelper;
        }

        public async Task<bool> SaveScore(IExamModel model)
        {
            return await Task.Run(() =>
            {              
                try
                {
                    List<IExamModel> list = new List<IExamModel>();
                    list.Add(model);
                    string json = JsonConvert.SerializeObject(list);
                    _redisHelper.Enqueue("阅卷出队列———examqueue", json);
                    Console.WriteLine(model.id);
                    //if (model.id == 3550543742890934272)
                    //{
                    //    throw new Exception();
                    //}
                    if (model.arbitration == 0)//一般题
                    {
                        _examTool.SaveExam(json);
                    }
                    if (model.arbitration == 1)//双评题
                    {
                        _examTool.SaveDouble(json);
                    }
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            });
        }

    }
}
